#include <stdio.h>
#include <stdbool.h>
#define size 15

void InitBoard(char (*board)[size])
{
    for(int i = 0; i < size; i++)
    {
        for(int j = 0; j < size; j++)
        {
            board[i][j] = '-';
        }
    }
}

void PrintBoard(char (*board)[size])
{
    printf("     ");
    for(int i = 0; i < size; i++)
    {
        printf("%5d",i+1);
    }
    printf("\n");

    for(int i = 0; i <size; i++)
    {
        printf("%5d",i+1);
        for(int j = 0; j < size; j++)
        {
            printf("%5c",board[i][j]);
        }
        printf("\n");
        printf("\n");
    }
}

bool MakeMove(char (*board)[size],int row,int column,char player)
{
    if(row < 0 || row >= size || column < 0 || column >= size)
    {
        printf("非法位置,请重新选择位置\n");
        return false;
    }

    if(board[row][column] != '-')
    {
        printf("该位置已经有棋子了,请重新选择位置\n");
        return false;
    }

    board[row][column] = player;
    return true;
}

bool CheckIsWin(char (*board)[size],int row,int column,char player)
{
    int direction[4][2] = {{1,0},{0,1},{1,1},{1,-1}};
    for(int i = 0; i < 4; i++)
    {
        int count = 1;
        int dx = row + direction[i][0];
        int dy = column + direction[i][1];
        while(dx < size && dx >= 0 && dy < size && dy >= 0 && board[dx][dy] == player)
        {
            count++;
            dx = dx + direction[i][0];
            dy = dy + direction[i][1];
            if(count >= 5)
            {
                return true;
            }
        }
        dx = row - direction[i][0];
        dy = column - direction[i][1];
        while(dx < size && dx >= 0 && dy < size && dy >= 0 && board[dx][dy] == player)
        {
            count++;
            dx = dx - direction[i][0];
            dy = dy - direction[i][1];
            if(count >= 5)
            {
                return true;
            }
        }
        if(count >= 5)
        {
            return true;
        }
    }
    return false;
}

bool CheckIsDraw(char (*board)[size],char player)
{
    for(int i = 0; i < size; i++)
    {
        for(int j = 0; j < size; j++)
        {
            if(board[i][j] == '-')
            return false;
        }
    }
    return true;
}

int main()
{
    char board[size][size] = {0};
    InitBoard(board);
    PrintBoard(board);
    char player = 'X';
    while(1)
    {
        PrintBoard(board);
        printf("请选手%c下棋\n",player);
        int row = 0;
        int column = 0;
        scanf("%d %d",&row,&column);
        if(MakeMove(board,row-1,column-1,player) == false)
        {
            continue;
        }
        if(CheckIsWin(board,row-1,column-1,player) == true)
        {
            PrintBoard(board);
            printf("选手%c赢得了比赛\n",player);
            break;
        }
        if(CheckIsDraw(board,player) == true)
        {
            PrintBoard(board);
            printf("平局！游戏结束\n");
            break;
        }
        player = (player == 'X') ? 'O' : 'X' ;
        
    }

    return 0;
}